package demoApiOfKboot.config;

import java.sql.SQLException;

import javax.annotation.Resource;
import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.env.Environment;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import com.alibaba.druid.pool.DruidDataSource;
import com.kayak.kboot.config.DruidDBConfig;
import com.kayak.kboot.dao.BaseDao;

/**
 *1.数据库连接池配置
 *2.多数据源对应多事务管理器
 *3.对外暴露多数据源comnDao 和 secondDao
 *4.一般情况下这个类只需要继承 DruidDBConfig类 无需写代码
 * @author caix
 *
 */
@Configuration
public class CustomDruidDBConfig extends DruidDBConfig{
	
	
	public DruidDataSource thirdDataSource(){
    	if(env.getProperty("third.datasource.url")==null){
    		return dataSource();
    	}
    	DruidDataSource datasource = new DruidDataSource();
    	datasource.setName(env.getProperty("third.datasource.nickName"));
        datasource.setUrl(env.getProperty("third.datasource.url"));
        datasource.setUsername(env.getProperty("third.datasource.userName"));
        datasource.setPassword(env.getProperty("third.datasource.password"));
        datasource.setDriverClassName(env.getProperty("third.datasource.driverClassName"));
        datasource.setDbType(env.getProperty("third.datasource.dbType"));
        

        // configuration
        datasource.setInitialSize(this.getInitialSize());
        datasource.setMinIdle(this.getMinIdle());
        datasource.setMaxActive(this.getMaxActive());
        datasource.setMaxWait(this.getMaxWait());
        datasource.setTimeBetweenEvictionRunsMillis(this.getTimeBetweenEvictionRunsMillis());
        datasource.setMinEvictableIdleTimeMillis(this.getMinEvictableIdleTimeMillis());
        datasource.setValidationQuery(this.getValidationQuery());
        datasource.setTestWhileIdle(this.isTestWhileIdle());
        datasource.setTestOnBorrow(this.isTestOnBorrow());
        datasource.setTestOnReturn(this.isTestOnReturn());
        datasource.setPoolPreparedStatements(this.isPoolPreparedStatements());
        datasource.setMaxPoolPreparedStatementPerConnectionSize(this.getMaxPoolPreparedStatementPerConnectionSize());
        try {
            datasource.setFilters(this.getFilters());
        } catch (SQLException e) {

        }
        datasource.setConnectionProperties(this.getConnectionProperties());
        return datasource;
    }
	
	
	public DataSourceTransactionManager transactionManager3() {
        return new DataSourceTransactionManager(thirdDataSource());
    }
    
    @Bean(name = "thirdDao")
    public BaseDao thirdDao(){
        return new BaseDao(transactionManager3());
    }
    
}

